; setup.asm
; setup window

	.include "common.inc"
	.include "global.inc"
	.include "window.inc"
	.include "execfun.inc"
	.include "cursor.inc"
	.include "screen.inc"
	.include "wutil.inc"

	.export setup

	.segment "RODATA"
	
setup_title: .byte 8, $53, $05, $14, $14, $09, $0E, $07, $13

tx_plus:	.byte 1, $2B
tx_minus:	.byte 1, $2D

tx_close:	.byte 5, $43, $0C, $0F, $13, $05

tx_frame:	.byte 5, $46, $12, $01, $0D, $05
tx_back:	.byte 10, $42, $01, $03, $0B, $07, $12, $0F, $15, $0E, $04

COLOR_CNT = 2

clrtxlo:	.byte <tx_frame, <tx_back
clrtxhi:	.byte >tx_frame, >tx_back
clrvllo:	.byte <tx_frclr, <tx_bkclr
clrvlhi:	.byte >tx_frclr, >tx_bkclr
clrstlo:	.byte <setfrcolor, <setbkcolor
clrsthi:	.byte >setfrcolor, >setbkcolor

tx_index:	.byte 1, 3

	.segment "DATA"

tx_start:
tx_frclr:	.byte 1, $30
tx_bkclr:	.byte 1, $30

f_setup:	.byte 0
h_setup:	.byte 0
h_color:	.res COLOR_CNT

cindex:		.res 1
coffset:	.res 1
	
	.segment "JMPTABLE"

cexec:		.res 2

	SETUP_WIDTH = 160
	SETUP_HEIGHT = 96

	.segment "CODE"

setup:		lda f_setup
			beq @create
			jmp frm_activate
@create:	; create main frame
			ldx #SETUP_WIDTH
			ldy #SETUP_HEIGHT
			jsr frm_create
			sta f_setup
			jsr frm_center
			jsr frm_getwin
			sta h_setup
			jsr win_realize
			lda #WSTYLE_BCKGND+WSTYLE_HASTITLE
			jsr win_setstyle
			lda #1					; lower case system font
			jsr win_setfont
			lda #<setup_title
			ldx #>setup_title
			jsr win_settext

			lda #0
			sta cindex
			lda #20
			sta coffset

@cclrbtn:
			; create label
			lda h_setup
			jsr win_create
			lda #WSTYLE_TXLEFT
			jsr win_setstyle
			ldx #4
			ldy coffset
			jsr win_setpos
			ldx #84
			ldy #12
			jsr win_setdim
			ldx cindex
			lda clrtxlo, x
			tay
			lda clrtxhi, x
			tax
			tya
			jsr win_settext
			
			; create minus button			
			lda h_setup
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #102
			ldy coffset
			jsr win_setpos
			ldx #14
			ldy #12
			jsr win_setdim
			lda #<tx_minus
			ldx #>tx_minus
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_minus_button
			ldx #>ex_minus_button
			jsr win_setexec
			lda cindex
			jsr win_setuser

			; create number window
			lda h_setup
			jsr win_create
			ldx cindex
			sta h_color, x
			lda #WSTYLE_BCKGND
			jsr win_setstyle
			ldx #120
			ldy coffset
			jsr win_setpos
			ldx #10
			ldy #12
			jsr win_setdim
			ldx cindex
			lda clrvllo, x
			tay
			lda clrvlhi, x
			tax
			tya
			jsr win_settext

			; create plus button			
			lda h_setup
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #134
			ldy coffset
			jsr win_setpos
			ldx #14
			ldy #12
			jsr win_setdim
			lda #<tx_plus
			ldx #>tx_plus
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_plus_button
			ldx #>ex_plus_button
			jsr win_setexec
			lda cindex
			jsr win_setuser
			
			lda #20
			clc
			adc coffset
			sta coffset
			inc cindex
			lda cindex
			cmp #COLOR_CNT
			bcs @donebtn
			jmp @cclrbtn

@donebtn:
			; create close button
			lda h_setup
			jsr win_create
			lda #WSTYLE_BORDER
			jsr win_setstyle
			ldx #58
			ldy #75
			jsr win_setpos
			ldx #44
			ldy #12
			jsr win_setdim
			lda #<tx_close
			ldx #>tx_close
			jsr win_settext
			lda #WTYPE_BUTTON
			jsr win_settype
			lda #<ex_close_button
			ldx #>ex_close_button
			jsr win_setexec
			
			; draw window			
			lda h_setup
			jsr win_draw
			lda h_setup
			jsr win_show			; to screen

			rts
			
ex_plus_button:
			jsr btncheck
			jsr win_getuser
			tax
			lda clrstlo, x
			sta cexec
			lda clrsthi, x
			sta cexec+1
			lda h_color, x
			pha
			lda tx_index, x
			tax
@inclp:		lda tx_start, x
			cmp #$37
			bcs atend
			adc #1
			sta tx_start, x
			jsr callcset
			pla
			jsr win_draw
			rts
			
atend:		pla
			rts

ex_minus_button:
			jsr btncheck
			jsr win_getuser
			tax
			lda clrstlo, x
			sta cexec
			lda clrsthi, x
			sta cexec+1
			lda h_color, x
			pha
			lda tx_index, x
			tax
@inclp:		lda tx_start, x
			cmp #$31
			bcc atend
			sbc #1
			sta tx_start, x
			jsr callcset
			pla
			jsr win_draw
			rts

ex_close_button:
			jsr btncheck
			lda f_setup
			jsr frm_destroy
			lda #0
			sta f_setup
			rts

callcset:	jmp (cexec)
